package service.impl;

import controller.UserController;
import dao.StudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.StudentService;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;
@Service("studentService")
public class StudentServicelmpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    public int count(pojo.Student student) {
        return studentDao.count(student);
    }
    @Override
    public List<pojo.Student> findAll(pojo.Student student, Integer offset, Integer pageSize) {
        if(student.getGender()!=null){
            if(student.getGender().equals("男")){
                student.setGender("0");
            }else if(student.getGender().equals("女")){
                student.setGender("1");
            }else if(student.getGender().equals("保密")){
                student.setGender("2");
            }
        }
        List<pojo.Student> list = studentDao.findAll(student, offset, pageSize);
        return list;
    }

    @Override
    public int add(pojo.Student student) {
        if(student.getDateOfBirth()!=null) {
            Calendar cal = Calendar.getInstance();//获取当前时间
            Calendar bir = Calendar.getInstance();//获取生日时间
            bir.setTime(student.getDateOfBirth());
            if (cal.before(bir)){
                return -1;
            }
            //取出当前年月日
            int nowYear = cal.get(Calendar.YEAR);
            int nowMonth = cal.get(Calendar.MONTH);
            int nowDay = cal.get(Calendar.DAY_OF_MONTH);
            //取出出生日期的年月日
            int birthYear = bir.get(Calendar.YEAR);
            int birthMonth = bir.get(Calendar.MONTH);
            int birthDay = bir.get(Calendar.DAY_OF_MONTH);

            int age = nowYear - birthYear;
            if(nowMonth < birthMonth || (nowMonth == birthMonth && nowDay < birthDay)){
                age--;
            }
            student.setAge(age);
        }
        student.setDelFlag("0");
        student.setCreateBy(UserController.getCurrentId());
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateBy(UserController.getCurrentId());
        student.setUpdateTime(LocalDateTime.now());
        return studentDao.add(student);
    }

    @Override
    public pojo.Student findById(Integer id) {
        return studentDao.findById(id);
    }

    @Override
    public int update(pojo.Student student) {
        if(student.getDateOfBirth()!=null) {
            Calendar cal = Calendar.getInstance();//获取当前时间
            Calendar bir = Calendar.getInstance();//获取生日时间
            bir.setTime(student.getDateOfBirth());
            if (cal.before(bir)){
                return -1;
            }
            //取出当前年月日
            int nowYear = cal.get(Calendar.YEAR);
            int nowMonth = cal.get(Calendar.MONTH);
            int nowDay = cal.get(Calendar.DAY_OF_MONTH);
            //取出出生日期的年月日
            int birthYear = bir.get(Calendar.YEAR);
            int birthMonth = bir.get(Calendar.MONTH);
            int birthDay = bir.get(Calendar.DAY_OF_MONTH);

            int age = nowYear - birthYear;
            if(nowMonth < birthMonth || (nowMonth == birthMonth && nowDay < birthDay)){
                age--;
            }
            student.setAge(age);
        }
        student.setUpdateBy(UserController.getCurrentId());
        student.setUpdateTime(LocalDateTime.now());
        return studentDao.update(student);
    }

    @Override
    public int delete(Integer id) {
        return studentDao.delete(id);
    }
}
